package logic

import (
	"context"
	"errors"
	"shell/model/mysql"

	"shell/shellrpc/internal/svc"
	"shell/shellrpc/shellrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HomeAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHomeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HomeAddLogic {
	return &HomeAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HomeAddLogic) HomeAdd(in *shellrpc.HomeAddRequest) (*shellrpc.HomeAddResponse, error) {
	// todo: add your logic here and delete this line

	var home mysql.Home
	mysql.DB.Debug().Where("title = ?", in.Title).Find(&home)
	if home.ID != 0 {
		return nil, errors.New("当前房源已存在，请更换")
	}

	m := mysql.Home{
		Title: in.Title,
		Desc:  in.Desc,
		Price: float64(in.Price),
		Uid:   int(in.Uid),
	}
	err := mysql.DB.Create(&m).Error
	if err != nil {
		return nil, err
	}

	return &shellrpc.HomeAddResponse{
		HomeId: int64(m.ID),
	}, nil
}
